package com.ag.lwc.action;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;

import com.ag.bean.TbCurrentEvent;
import com.ag.bean.TbEvent;
import com.ag.bean.TbLog;
import com.ag.bean.TbLogPlanStep;
import com.ag.bean.TbLogType;
import com.ag.common.BaseAction;
import com.ag.common.EventUtil;
import com.ag.lwc.service.TbLogTypeService;
import com.ag.lwc.service.impl.LogService;
import com.ag.zrt.dao.TbCurrentEventDAO;
import com.ag.zrt.service.ILogPlanStepService;

//发送日志的action
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class SendLogAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4058316436607343603L;
	// 发送
	public String mess; // 传进了的日志内容
	public String logtype; // 日志类型
	public ILogPlanStepService service;
	public TbLogTypeService logTypeService;
	public LogService logService;
	private TbCurrentEventDAO currentEventDAO;
	public String message;
	public String eventId = null;

	public String getEventId() {
		return eventId;
	}

	public void setEventId(String eventId) {
		this.eventId = eventId;
	}

	public String getCurrentPsId() // 获取当前步骤id
	{
		String psId = null;
		if (eventId == null) {
			eventId = (String) this.getSession().getAttribute(
					EventUtil.PROCESSING_EVENT_ID);
		}
		System.out
				.println(eventId
						+ "******************保存日志的事件ID*********************************************");

		psId = currentEventDAO.findById(eventId).getCurStepId();

		System.out
				.println(psId
						+ "******************保存日志的当前步骤ID*********************************************");
		// Session session;
		//
		// session = currentEventDAO.getSessionFactory().openSession();
		//
		// if (eventId == null) {
		// List<TbCurrentEvent> list = session.createSQLQuery(
		// "select * from TB_CURRENT_EVENT").addEntity(
		// TbCurrentEvent.class).list();
		// System.out.println(list.size());
		// TbCurrentEvent currentevent = list.get(0);
		// //
		// System.out.println("万才的action：CurrentPsContentAction: 步骤id："+currentevent.getEId());
		// psId = currentevent.getCurStepId(); // 当前步骤获取id
		//
		// } else {
		// TbCurrentEvent currentevent = (TbCurrentEvent) session.get(
		// TbCurrentEvent.class, eventId);
		// psId = currentevent.getCurStepId();
		//
		// }
		// session.close();
		if (psId == null) {
			System.out.println("出错啦，没有步骤id");
		}

		return psId;
	}

	public String send() {
		String psId = null;
		try {
			System.out.println("*****mess: " + mess + " logtype: " + logtype
					+ " psId: " + psId);
			psId = this.getCurrentPsId();
			System.out.println("万才的SendLogAction： 发送时的预案步骤id：" + psId);
			// TbLogPlanStep tbLogPlanStep = service.findById(psId);
			// if(tbLogPlanStep!=null)
			// {
			if (psId != null) {
				TbLog tbLog = new TbLog(); // 新增一个日志
				System.out.println("日志类型： ");
				TbLogPlanStep tbLogPlanStep = new TbLogPlanStep();
				tbLogPlanStep.setPsId(psId);
				tbLog.setTbLogPlanStep(tbLogPlanStep); // 日志所属的步骤环节,对应在rpc中可以查出日志对应的环节步骤名
				
				TbEvent e = new TbEvent();
				e.setEId(eventId);
				tbLog.setTbEvent(e);
				tbLog.setLogContent(mess);

				TbLogType mylogType = new TbLogType();
				mylogType.setLtId(logtype);
				tbLog.setTbLogType(mylogType);

				tbLog.setLogTime(new Date());

				logService.save(tbLog); // 保存日志
				System.out.println("设置完步骤");
				System.out.println("**********SendLogAction:**********");
			} else {
				System.out.println("**********当前环节ID为空**************************************");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		message = "send";
		return "send";
	}

	public String getMess() {
		return mess;
	}

	public void setMess(String mess) {
		this.mess = mess;
	}

	public String getLogtype() {
		return logtype;
	}

	public void setLogtype(String logtype) {

		this.logtype = logtype;
		System.out.println("日志类型：" + this.logtype);
	}

	public ILogPlanStepService getService() {
		return service;
	}

	public void setService(ILogPlanStepService service) {
		this.service = service;
	}

	public TbLogTypeService getLogTypeService() {
		return logTypeService;
	}

	public void setLogTypeService(TbLogTypeService logTypeService) {
		this.logTypeService = logTypeService;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}

	public TbCurrentEventDAO getCurrentEventDAO() {
		return currentEventDAO;
	}

	public void setCurrentEventDAO(TbCurrentEventDAO currentEventDAO) {
		this.currentEventDAO = currentEventDAO;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
